package youcaihuaoa.weetop.com.vm.main

import android.app.Application
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.NoticeBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class NotifyMessageViewModel(application: Application) : GLBaseViewModel(application) {

    val uiChangeEvent = UiChangeEvent()

    class UiChangeEvent {
        val dataEvent = SingleLiveEvent<List<NoticeBean>>()
    }

    val clickAllEditCommand = BindingCommand<Any>(BindingAction {
        userMessageNoticeEdit("2", "")
    })

    fun getUserMessageNotice() {
        showLoadingDialog()
        UserServiceFactory.getUserMessageNotice()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<NoticeBean>>>(this) {
                override fun onNext(t: BaseResponse<List<NoticeBean>>) {
                    dismissLoadingDialog()
                    t.data ?: return
                    uiChangeEvent.dataEvent.postValue(t.data)
                }
            })
    }

    fun userMessageNoticeEdit(type: String, id: String) {
        showLoadingDialog()
        UserServiceFactory.userMessageNoticeEdit(
            mapOf(
                "type" to type,
                "id" to id
            )
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
            override fun onNext(t: BaseResponse<String>) {
                getUserMessageNotice()
            }

        })
    }
}